package com.xdcplus.web.service.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.CraftCheckOrderTargetMapper;
import com.xdcplus.web.common.pojo.entity.CraftCheckOrderTarget;
import com.xdcplus.web.common.pojo.dto.CraftCheckOrderTargetDTO;
import com.xdcplus.web.common.pojo.dto.CraftCheckOrderTargetFilterDTO;
import com.xdcplus.web.common.pojo.vo.CraftCheckOrderTargetVO;
import com.xdcplus.web.common.pojo.query.CraftCheckOrderTargetQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.CraftCheckOrderTargetService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * 点检工单明细表(CraftCheckOrderTarget)表服务实现类
 *
 * @author Man.Chen
 * @since 2024-12-03 17:43:56
 */
@Slf4j
@Service("craftCheckOrderTargetService")
public class CraftCheckOrderTargetServiceImpl extends BaseServiceImpl<CraftCheckOrderTargetMapper, CraftCheckOrderTarget, CraftCheckOrderTarget, CraftCheckOrderTargetVO> implements CraftCheckOrderTargetService {

    @Autowired
    protected CraftCheckOrderTargetMapper craftCheckOrderTargetMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveCraftCheckOrderTarget(CraftCheckOrderTargetDTO craftCheckOrderTargetDTO) {

        CraftCheckOrderTarget craftCheckOrderTarget = new CraftCheckOrderTarget();
        BeanUtil.copyProperties(craftCheckOrderTargetDTO, craftCheckOrderTarget);
        craftCheckOrderTarget.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(craftCheckOrderTarget);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateCraftCheckOrderTarget(CraftCheckOrderTargetDTO craftCheckOrderTargetDTO) {

        CraftCheckOrderTarget craftCheckOrderTarget = this.getById(craftCheckOrderTargetDTO.getId());
        if (ObjectUtil.isNull(craftCheckOrderTarget)) {
            log.error("updateCraftCheckOrderTarget() The CraftCheckOrderTarget does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(craftCheckOrderTargetDTO, craftCheckOrderTarget);
        craftCheckOrderTarget.setUpdatedUser(craftCheckOrderTargetDTO.getUpdatedUser());
        craftCheckOrderTarget.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(craftCheckOrderTarget);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<CraftCheckOrderTarget> craftCheckOrderTargetList) {

        craftCheckOrderTargetList.forEach(craftCheckOrderTarget -> {
            CraftCheckOrderTarget craftCheckOrderTargetParam = new CraftCheckOrderTarget();
            craftCheckOrderTargetParam.setId(craftCheckOrderTarget.getId());
            if (ObjectUtil.isNotNull(craftCheckOrderTarget.getId())) {
                craftCheckOrderTarget.setId(craftCheckOrderTarget.getId());
                craftCheckOrderTarget.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<CraftCheckOrderTarget> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(CraftCheckOrderTarget::getId, craftCheckOrderTarget.getId());
                update(craftCheckOrderTarget, lambdaUpdate);
            } else {
                craftCheckOrderTarget.setCreatedTime(new Timestamp(DateUtil.current()));
                save(craftCheckOrderTarget);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<CraftCheckOrderTargetDTO> craftCheckOrderTargetDTOList) {

        List<CraftCheckOrderTarget> craftCheckOrderTargetList = BeanUtils.copyProperties(craftCheckOrderTargetDTOList, CraftCheckOrderTarget.class);
        return saveOrUpdateBatch(craftCheckOrderTargetList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteCraftCheckOrderTargetLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        CraftCheckOrderTarget craftCheckOrderTarget = this.getById(id);

        if (ObjectUtil.isNull(craftCheckOrderTarget)) {
            log.error("deleteCraftCheckOrderTarget() The CraftCheckOrderTarget does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteCraftCheckOrderTargetByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			CraftCheckOrderTarget craftCheckOrderTarget = this.getById(id);
			if (ObjectUtil.isNull(craftCheckOrderTarget)) {
				log.error("deleteCraftCheckOrderTarget() The CraftCheckOrderTarget does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<CraftCheckOrderTarget> queryCraftCheckOrderTargetList(CraftCheckOrderTargetFilterDTO craftCheckOrderTargetFilterDTO) {
        CraftCheckOrderTargetQuery craftCheckOrderTargetQuery = BeanUtil.copyProperties(craftCheckOrderTargetFilterDTO, CraftCheckOrderTargetQuery.class);

        return craftCheckOrderTargetMapper.queryCraftCheckOrderTarget(craftCheckOrderTargetQuery);
    }

    @Override
    public List<CraftCheckOrderTargetVO> queryCraftCheckOrderTargetVOList(CraftCheckOrderTargetFilterDTO craftCheckOrderTargetFilterDTO) {
        return this.objectConversion(queryCraftCheckOrderTargetList(craftCheckOrderTargetFilterDTO));
    }

    @Override
    public PageVO<CraftCheckOrderTargetVO> queryCraftCheckOrderTarget(CraftCheckOrderTargetFilterDTO craftCheckOrderTargetFilterDTO) {
        PageVO<CraftCheckOrderTargetVO> pageVO = new PageVO<>();

        if (craftCheckOrderTargetFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(craftCheckOrderTargetFilterDTO.getCurrentPage(), craftCheckOrderTargetFilterDTO.getPageSize(),
                    craftCheckOrderTargetFilterDTO.getOrderType(), craftCheckOrderTargetFilterDTO.getOrderField());
        }

        List<CraftCheckOrderTarget> craftCheckOrderTargetList = queryCraftCheckOrderTargetList(craftCheckOrderTargetFilterDTO);

        PageInfo<CraftCheckOrderTarget> pageInfo = new PageInfo<>(craftCheckOrderTargetList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(craftCheckOrderTargetList));

        return pageVO;
    }

    @Override
    public CraftCheckOrderTargetVO queryCraftCheckOrderTargetById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
